package net.area80.composer.form
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	import net.area80.composer.core.Composer;
	import net.area80.utils.StringUtils;
	
	import org.osflash.signals.Signal;

	public class TextFieldComposer extends FormComposer
	{
		private var _tf:TextField;
		private var _fieldBG:MovieClip;
		private var _enabled:Boolean = true;
		private var _defaultMessage:String = "";
		private var _isFocus:Boolean = false;
		public var autoTrim:Boolean = true;
		private var multiline:Boolean = true;
		public var SIGNAL_ONPRESSENTER:Signal = new Signal(TextFieldComposer);
		private var initMessage:String = "";
		private var isInit:Boolean = false;

		public function TextFieldComposer(textField:DisplayObject, defaultMessage:String="", initMessage:String="", fieldBG:DisplayObject = null, multiline:Boolean=false)
		{
			this.initMessage = initMessage;
			_tf = textField as TextField;
			_fieldBG = fieldBG as MovieClip;
			_defaultMessage = defaultMessage;
			_tf.addEventListener(Event.CHANGE, changeHandler);
			this.multiline = multiline;

			
			if(_fieldBG) _fieldBG.gotoAndStop(1);

			if (!_tf.stage)
			{
				_tf.addEventListener(Event.ADDED_TO_STAGE, initTextListener);
			}
			else
			{
				initTextListener();
			}
			super(_tf);

		}
		
		private function changeHandler (e:Event):void {
			if(!multiline) {
			
				_tf.text = StringUtils.initHTMLString(_tf.text,false);
			}
			dispatchEvent(new Event("change"));
		}
		
		public function reset ():void {
			_tf.text = _defaultMessage;
		}
		public function setText (t:String):void {

			if(!isInit) initMessage = t;
			_tf.text = (multiline) ? t: StringUtils.initHTMLString(t,false);
		}
		
		public override function get value ():* {
			return (trim(_tf.text)==trim(_defaultMessage)) ? "" : trim(_tf.text);
		}
		
		public function focus():void
		{
			_tf.stage.focus = _tf;
			//_tf.setSelection(0, _tf.text.length);
			
			
		}
		private function onKeyDown (e:KeyboardEvent):void {
			if(e.keyCode==Keyboard.ENTER) {
				if(!multiline) {
					_tf.stage.focus = null;
				}
				SIGNAL_ONPRESSENTER.dispatch(this);
			}
		}
		
		public function set enabled(b:Boolean):void
		{
			if (b != _enabled)
			{
				if (b)
				{
					if (_fieldBG)
						_fieldBG.gotoAndStop(((_isFocus) ? 2 : 1));
					_tf.mouseEnabled = true;
				}
				else
				{
					if (_fieldBG && _fieldBG.totalFrames > 2)
						_fieldBG.gotoAndStop(3);
					_tf.mouseEnabled = false;
				}
			}
			_enabled = b;
			
		}
		
		public function get enabled():Boolean
		{
			return _enabled;
		}
		
		

		protected function initTextListener(e:Event=null):void
		{
			_tf.addEventListener(FocusEvent.FOCUS_IN, doFocus);
			_tf.addEventListener(FocusEvent.FOCUS_OUT, doFocus);
			isInit = true;
			_tf.text = initMessage;
			_tf.addEventListener(Event.REMOVED_FROM_STAGE, removeTextListener);
		}

		protected function removeTextListener(e:Event):void
		{
			_tf.removeEventListener(FocusEvent.FOCUS_IN, doFocus);
			_tf.removeEventListener(FocusEvent.FOCUS_OUT, doFocus);
			_tf.removeEventListener(Event.REMOVED_FROM_STAGE, removeTextListener);
			if(_tf.stage) _tf.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			_tf.addEventListener(Event.ADDED_TO_STAGE, initTextListener);
		}


		
		protected function trim (msg:String):String {
			return (autoTrim) ? StringUtils.trim(msg) : msg;
		}
		protected function doFocus(e:Event):void
		{
			_isFocus = (e.type == FocusEvent.FOCUS_IN);
			if(_isFocus) {
				if(trim(_tf.text) == trim(_defaultMessage)) {
					_tf.text = "";
				}
				_tf.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			} else {
				if(trim(_tf.text) == "") {
					_tf.text = trim(_defaultMessage);
				}
				_tf.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			}
			if (_fieldBG)
			{
				_fieldBG.gotoAndStop(((_isFocus) ? 2 : 1));
			}
		}
	}
}